// const md5 = require('../static/utils/md5')
import md5 from '@/static/utils/md5.min.js'
// 参数排序
function objKeySort(obj) {
	var newkey = Object.keys(obj).sort();
	var newObj = {};
	for (var i = 0; i < newkey.length; i++) {
			newObj[newkey[i]] = obj[newkey[i]];
	}
	return newObj;
}

function sign() {
	let data = {}
	// #ifdef H5
	let ua = navigator.userAgent.toLowerCase()
	if (ua.match(/MicroMessenger/i) == "micromessenger") {
		data.port = 'wechat'
	}else{
		data.port = 'h5'
	}
	// #endif
	// #ifdef MP-WEIXIN
	data.port = 'wechat'
	// #endif
	// #ifdef APP-PLUS
	switch(uni.getSystemInfoSync().platform){
		case 'android':
			data.port = 'android'
		  break;
		case 'ios':
			data.port = 'ios'
		  break;
	}
	// #endif
	data.timestamp = Math.round(new Date() / 1000)
	data.key = uni.getStorageSync('userInfo')?JSON.parse(uni.getStorageSync('userInfo')).token:''
	// #ifdef MP-WEIXIN
	data.appid = uni.getExtConfigSync().appid
	// #endif
	return data
}

let env = process.env.NODE_ENV

let baseUrl

let isTest = false
if(isTest){
	baseUrl = 'https://tst.shengejiaju.cn/'
}else{
	baseUrl = 'https://master.shengejiaju.cn/'
}

const install = (Vue, vm) => {
	Vue.prototype.$u.http.setConfig({
		baseUrl: baseUrl,
		loadingText: '正在加载中',
		loadingTime: 200,
		header: {
			'content-type':'application/x-www-form-urlencoded'
		}
	});
	// 请求拦截
	Vue.prototype.$u.http.interceptor.request = (config) => {
		if(config.url.indexOf('deng51')!=-1){
			return
		}
		if(!config.data.key){
			config.data.key = sign().key
		}
		config.data.port = sign().port
		config.data.timestamp = sign().timestamp
		// #ifdef MP-WEIXIN
		// config.data.appid = sign().appid
		// #endif
		
		// 获取签名开始
		let obj = 'jjds2020+'
		let newObj = objKeySort(config.data)
		for (let i in newObj){
			obj += i + '=' + newObj[i] +'&'
		}
		obj = obj.substring(0, obj.length - 1)
		obj += '+jjds2020'
		console.log(obj)
		// obj = md5.hexMD5(obj)
		// obj = obj.toUpperCase()
		obj = md5(obj).toUpperCase()
		// 获取签名结束
		config.data.sign = obj
		return config;
	}
	let num = 0
	// 响应拦截
	Vue.prototype.$u.http.interceptor.response = (res) => {
		if(res.status==1){ // status为灯具api返回值统一处理
			return res
		}else if(res.status && res.status!=1){
			vm.$u.toast(res.message);
			return false;
		}else if(res.code == 200) {
			return res;
		} else if(res.code == 201) {
			num++
			// #ifdef APP-PLUS
			// 判断APP端是否在审核，如果在审核则弹出登录提醒，否则直接跳转登录页面
			if(uni.getStorageSync('review')){
				uni.showModal({
					content: '该功能需要登录体验，是否登录？',
					confirmText: '登录',
					cancelText: '取消',
					success: (res) => {
						if(res.confirm){
							uni.navigateTo({
								animationDuration: 500,
								url: '/pages/login/app-login'
							})
						}
					}
				})
			}else{
				vm.$u.toast('请先登录');
				uni.removeStorageSync('userInfo')
				setTimeout(() => {
					uni.navigateTo({
						animationDuration: 500,
						url: '/pages/login/app-login',
						success: () => {
							num = 0
						}
					})
				},1500)
			}
			// #endif
			// #ifdef H5
			vm.$u.toast('请先登录');
			uni.removeStorageSync('userInfo')
			setTimeout(() => {
				uni.reLaunch({
					animationDuration: 500,
					url: '/pages/login/login'
				})
			}, 1500)
			// #endif
			// #ifdef MP-WEIXIN
			if(uni.getStorageSync('review') || uni.getExtConfigSync().appid==='wx9528157f77bff5d2'){
				if(num==1){
					uni.navigateTo({
						animationDuration: 500,
						url: '/pages/login/login'
					})
				}
			}else{
				if(num==1){
					vm.$u.toast('请先登录');
					uni.removeStorageSync('userInfo')
					setTimeout(() => {
						uni.navigateTo({
							animationDuration: 500,
							url: '/pages/login/login',
							success: () => {
								num = 0
							}
						})
					},1500)
				}
			}
			// #endif
			return false;
		}else if(res.code == 204){
			vm.$u.toast(res.error);
			return false;
		}else{
			vm.$u.toast('网络打盹啦，请稍等');
			return false;
		}
	}
}

export default{
	install,sign
}